package com.cf.zookeeper.controller;

import com.cf.constant.EnumReturnCode;
import com.cf.crudweb.controller.CommonController;
import com.cf.exception.BizException;
import com.cf.vo.ResponseVo;
import com.cf.zookeeper.controller.vo.Node;
import com.cf.zookeeper.model.Zookeeper;
import com.cf.zookeeper.service.ZkCache;
import com.cf.zookeeper.service.ZkManager;
import com.cf.zookeeper.service.ZookeeperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * boot-awm-ZookeeperController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/25 下午 03:31
 */
@RequestMapping("/zookeeper")
public class ZookeeperController extends CommonController<Zookeeper, ZookeeperService> {
    @Autowired
    private ZkCache zkCache;

    @Override
    protected String getPageDir() {
        return "zookeeper";
    }

    @ResponseBody
    @RequestMapping("/data")
    public ResponseVo<List<Node>> data(long id, String path) {
        ZkManager zkManager = zkCache.get(id);
        if (zkManager == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "获取数据失败！");

        List<String> children = zkManager.getChildren(path);
        if (children != null && children.size() > 0) {
            return ResponseVo.success(children.stream().map(ch -> {
                return new Node(id, ch, "/".equals(path) ? ("/" + ch) : path + "/" + ch);
            }).collect(Collectors.toList()), "获取节点数据成功！");
        } else {
            return ResponseVo.success(Collections.emptyList(), "获取节点数据成功！");
        }
    }

    @ResponseBody
    @RequestMapping("/detail")
    public ResponseVo<List<Node>> detail(long id, String path) {
        ZkManager zkManager = zkCache.get(id);
        if (zkManager == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "获取数据失败！");
        String data = zkManager.getData(path);
        Map<String, String> nodeMeta = zkManager.getNodeMeta(path);
        nodeMeta.put("id", id + "");
        nodeMeta.put("path", path);
        nodeMeta.put("data", data);
        return ResponseVo.success(nodeMeta, "获取数据成功！");
    }


    @ResponseBody
    @RequestMapping("/set")
    public ResponseVo<Boolean> set(long id, String path, String data) {
        ZkManager zkManager = zkCache.get(id);
        if (zkManager == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "获取数据失败！");
        boolean b = zkManager.setData(path, data);
        return ResponseVo.success(b, "设置数据操作成功！");
    }

    @ResponseBody
    @RequestMapping("/create")
    public ResponseVo<Boolean> create(long id, String path, String name, String data,int type) {
        ZkManager zkManager = zkCache.get(id);
        if (zkManager == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "获取数据失败！");
        boolean node = zkManager.createNode(path, name, data,type);
        return ResponseVo.success(node, "设置数据操作成功！");
    }

    @ResponseBody
    @RequestMapping("/remove")
    public ResponseVo<Boolean> remove(long id, String path) {
        ZkManager zkManager = zkCache.get(id);
        if (zkManager == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "获取数据失败！");
        boolean node = zkManager.deleteNode(path);
        return ResponseVo.success(node, "删除节点成功！");
    }


}
